package sms_interface;


import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Sending_sms {
        /**
         * Sending of sms using files rather than mysql database. 
         * @param room_id room where electricity is being wasted.
         * @param Appliance_id Appliance which is wasting electricity. 
         * @return 1 if everything is working fine.
         * @throws IOException
         * @throws InterruptedException
         */
        static public int get_authority(String room_id,String Appliance_id) throws IOException, InterruptedException
        {
                //while(true)
                //{
                        BufferedReader br=null;
                        //BufferedReader br2=null;
                        PrintWriter pr=null;
               //         try{
                        File f1=new File("Authority_Database.txt");
                        File f2=new File("Log_sms_sent.txt");
                        br=new BufferedReader(new FileReader(f1));
                        pr=new PrintWriter(new FileWriter(f2,true));
                        //br2=new BufferedReader(new FileReader(f2));
                 /*       }catch(FileNotFoundException fnf)
                        {
                                System.out.println("File Named: Authority Database was not found.");
                                return 0;
                        }*/
                        String str;
                        while((str=br.readLine())!=null)
                        {
                                //System.out.println(str);
                                String Authority_Name=null;
                                String number=null;
                                String message;
                                String[] arr1=str.split(" ");
                                //System.out.println(arr1[0]);
                                if( room_id.equalsIgnoreCase(arr1[0])==true)
                                {
                                        Authority_Name=arr1[1]+" "+arr1[2]+" "+arr1[3];
                                        number=arr1[4];
                                //message="This is to inform you that electricity wastage has been detected in room no. "+room_id+". Appliance with the wastage has ID as "+Appliance_id+". Being the concerned authority("+Authority_Name+") for this room, we request you to switch off the appliance ASAP.\nBy:-IIIT Delhi";
                                message="hello world";
                                System.out.println(message);
                                send_sms(number,message);
                                pr.write("Update sms_sent set sent_time="+now()+", sent='y' where sms sent to "+Authority_Name+" with Room_ID "+room_id+"\n");
                //pr.println();
                                break;
                        }
                }
                        pr.close();
                        br.close();
        return 1;
        }
              
        
        /**
    	 * Code to send sms using the gsm gateway. This code has been taken from google repository IIIT-D ivrs.
    	 * @param number number to which sms to be sent.
    	 * @param message message to be sent to the user.
    	 * @throws InterruptedException
    	 */
    	       public static void send_sms(String number, String message) throws InterruptedException
        {
                String arr[] =message.split("\\s");
                String reformed ="";
                for(int i = 0;i<arr.length;i++)
                {
                        reformed = reformed +"%20" + arr[i];
                }
                System.out.println(reformed);
                System.out.println("Query formed:\nwget -o smssent -r --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-send.html?number="+number.toString()+"&smsencode="+reformed+"&send=Send");
                /*try {
                        Runtime.getRuntime().exec("wget -o smssent -r --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-send.html?number=0"+number.toString()+"&smsencode="+reformed+"&send=Send").waitFor();
                } catch (IOException e) {
                        
                        e.printStackTrace();
                }*/
        }
        
    	       
    	       
    	   	/**
    	   	 * Returns the time at the current moment.
    	   	 * Ref:http://stackoverflow.com/questions/833768/java-code-for-getting-current-time
    	   	 * @return Time in String.
    	   	 */
    	static public String now()
        {
        Calendar cal = Calendar.getInstance();
        cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return (String)sdf.format(cal.getTime());
    
        }
        
        /*static public void main(String[] args) throws IOException, InterruptedException
        {
                get_authority("CR3","F2");
                //get_authority("CR2","A2");
                //get_authority("CR1","B2");
                
        }*/
}
